package com.workpaper.app.plan.view;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.AppConsts;
import com.workpaper.app.consts.common.CommonViewConsts;
import com.workpaper.app.consts.plan.DailyPlanViewConsts;
import com.workpaper.app.plan.business.DailyPlanBusiness;
import com.workpaper.app.plan.entity.DailyPlan;
import com.workpaper.app.utils.platform.SWTPlatformUtils;
/**
 * 日常计划新增窗口
 * @author zyj
 *
 */
public class DailyPlanAddDialog extends Dialog {
	/**
	 * 日期选择器
	 */
	protected DateTime dateSelector;
	/**
	 * 内容文本
	 */
	protected StyledText contentText;
	/**
	 * 执行人
	 */
	protected Text executorText; 
	/**
	 * 备注
	 */
	protected StyledText remarkText;
	
	public DailyPlanAddDialog(Shell parentShell) {
		super(parentShell);
	}

	
	/**
	 * 配置窗口标题、图标等信息
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		// TODO Auto-generated method stub
		super.configureShell(newShell);
		newShell.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanAddViewTitle));	//标题
//		newShell.setImage(null);	//图标
	}


	
	@Override
	protected Point getInitialSize() {
		return new Point(400, 400);
	}

	/**
	 * 内容设置
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout layout = new GridLayout(2, false);
		layout.marginRight = 6;
		container.setLayout(layout);

		//创建用日期：标签、日期控件 输入框
		Label dsNameLabel = new Label(container, SWT.NONE);
		dsNameLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Date));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		dsNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		dsNameLabel.setImage(CacheImage.getAppImage(ImagePath.LOGIN_USERNAME_ICO));
		dateSelector = new DateTime(container, SWT.DATE);
		dateSelector.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		//创建内容：
		Label contentLabel = new Label(container, SWT.NONE);
		contentLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Content));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		contentLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		contentText = new StyledText(container, SWT.BORDER);
		contentText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		//创建执行人：
		Label executorLabel = new Label(container, SWT.NONE);
		executorLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Executor));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		executorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		executorText = new Text(container, SWT.BORDER);
		executorText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		//创建备注：
		Label remarkLabel = new Label(container, SWT.NONE);
		remarkLabel.setText(ViewInfoCache.get(DailyPlanViewConsts.DailyPlanEditText_Remark));
		//水平右对齐，垂直居中，水平方向不填充空白，垂直方向不填充空白， 跨1行，跨1列
		remarkLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		remarkText = new StyledText(container, SWT.BORDER);
		remarkText.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		
		return container;
	}


	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		//保存按钮
//		Button saveBtn = 
		createButton(parent, IDialogConstants.OK_ID, ViewInfoCache.get(CommonViewConsts.ButtonText_Save), false);
		//取消按钮，默认选中
//		Button cancelBtn = 
		createButton(parent, IDialogConstants.CANCEL_ID, ViewInfoCache.get(CommonViewConsts.ButtonText_Cancel), true);
	}


	@Override
	protected void okPressed() {
		DailyPlan dailyPlan = extractPlanData();
		
		DailyPlanBusiness.getInstance().addNewDailyPlan(dailyPlan);
		
		SWTPlatformUtils.getViewByID(AppConsts.DAILY_PLAN_MANAGE_VIEW, DailyPlanManageView.class).refreshTableData();
		
		super.okPressed();
	}

	/**
	 * 抽取数据
	 * @return
	 */
	protected DailyPlan extractPlanData() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, dateSelector.getDay());
		calendar.set(Calendar.MONTH, dateSelector.getMonth());
		calendar.set(Calendar.YEAR, dateSelector.getYear());
		
		Date planDate = calendar.getTime();
		String executor = executorText.getText();
		String content = contentText.getText();
		String remark = remarkText.getText();
		
		
		DailyPlan dailyPlan = new DailyPlan();
		dailyPlan.setContent(content);
		dailyPlan.setExecutor(executor);
		dailyPlan.setPlanDate(planDate);
		dailyPlan.setRemark(remark);
		return dailyPlan;
	}


	@Override
	protected void cancelPressed() {
		super.cancelPressed();
	}

}
